import cn.hutool.core.bean.BeanUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.demo.Application;
import com.example.demo.controller.work_hour_and_operation;
import com.example.demo.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @ClassName test
 * @Description
 * @date 2023/5/12 10:03
 * @Author yanceysong
 * @Version 1.0
 */
@SpringBootTest(classes= Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
@Slf4j
public class test {
    @Autowired
    private WorkService workService;
    @Test
    public void excel2db() {
        String filePath = "C:\\Users\\BG264\\Desktop\\111.xlsx";
        try {
            int id = 17287;
            ExcelReader reader = ExcelUtil.getReader(new File(filePath),"转动工序");
            List<work_hour_and_operation> all = reader.readAll(work_hour_and_operation.class);
            log.info("从Excel读取数据成功，共" + all.size() + "条数据");
            work_hour_and_operation replace = new work_hour_and_operation();
            for (work_hour_and_operation w : all) {
                if (w.material_name != null && w.material_name.length() > 0) {
                    BeanUtil.copyProperties(w, replace);
                    continue;
                }
                w.setWork_code("2D-SD210001-10");
                w.setId(id++);
                w.setFit_level(replace.getFit_level());
                w.setMaterial_code(replace.getMaterial_code());
                w.setPhoto_code(replace.getPhoto_code());
                w.setMaterial_name(replace.getMaterial_name());
                w.setNumber(replace.getNumber());
                w.setUnit(replace.getUnit());
                w.setRoad_branch(replace.getRoad_branch());
                workService.save(w);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
